package com.demo.controller;


import com.demo.dto.CommentFormDTO;
import com.demo.dto.Result;
import com.demo.entity.Comment;
import com.demo.query.CommentListQuery;
import com.demo.service.CommentService;
import com.demo.utils.SystemConstants;
import com.demo.vo.CommentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zeng
 * @since 2022-03-23
 */
@Api(tags = "评论模块")
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
    CommentService commentService;

    @ApiOperation("在一篇回答下进行评论")
    @PostMapping("/")
    Result<Comment> postComment(@RequestBody CommentFormDTO commentFormDTO) {
        return commentService.postComment(commentFormDTO);
    }

    @ApiOperation("删除一条评论")
    @DeleteMapping("/{comment_id}")
    Result<Comment> postComment(@PathVariable("comment_id") Integer commentId) {
        return commentService.delete(commentId);
    }

    @ApiOperation("获取一篇回答下的评论及其下面的3个回复")
    @GetMapping("/common/page")
    Result<List<CommentVO>> getListByAnswerId(
            @RequestParam("answer_id") Integer answerId,
            @RequestParam(value = "current", defaultValue = "1") Integer current
    ) {
        CommentListQuery commentListQuery = new CommentListQuery();
        commentListQuery.setAnswerId(answerId);
        commentListQuery.setCurrentPage(current);
        commentListQuery.setPageSize(SystemConstants.MAX_PAGE_SIZE);

        return commentService.getListByAnswerId(commentListQuery);
    }
}

